package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Personnel;
import com.ruoyi.system.service.IPersonnelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户Controller
 * 
 * @author 留白
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/system/personnel")
public class PersonnelController extends BaseController
{
    private String prefix = "system/personnel";

    @Autowired
    private IPersonnelService personnelService;

    @RequiresPermissions("system:personnel:view")
    @GetMapping()
    public String personnel()
    {
        return prefix + "/personnel";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("system:personnel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Personnel personnel)
    {
        startPage();
        List<Personnel> list = personnelService.selectPersonnelList(personnel);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("system:personnel:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Personnel personnel)
    {
        List<Personnel> list = personnelService.selectPersonnelList(personnel);
        ExcelUtil<Personnel> util = new ExcelUtil<Personnel>(Personnel.class);
        return util.exportExcel(list, "用户数据");
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:personnel:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Personnel> util = new ExcelUtil<Personnel>(Personnel.class);
        List<Personnel> userList = util.importExcel(file.getInputStream());
        String message = personnelService.importPersonnel(userList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:personnel:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Personnel> util = new ExcelUtil<Personnel>(Personnel.class);
        return util.importTemplateExcel("人员数据");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:personnel:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Personnel personnel)
    {
        return toAjax(personnelService.insertPersonnel(personnel));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("system:personnel:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Personnel personnel = personnelService.selectPersonnelById(id);
        mmap.put("personnel", personnel);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:personnel:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Personnel personnel)
    {
        return toAjax(personnelService.updatePersonnel(personnel));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("system:personnel:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(personnelService.deletePersonnelByIds(ids));
    }
}
